/*
 * Copyright 2010 Gaurav Saxena
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.gwtstructs.gwt.client.widgets.autocompleterTextbox;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class provides support for custom HTML based suggestions by the use of interface {@link ICustomSuggestion}.
 * The keys on which filtration is based and those which appear in the textbox will still be strings but the
 * suggestions can have custom HTML
 * @author Gaurav Saxena
 *
 */
public class CustomSuggestionAutoCompleterTextBox extends AutoCompleterTextBox {
	
	private List<ICustomSuggestion> suggestions;
	/**
	 * @param suggestions list of objects of {@link ICustomSuggestion} 
	 * @param position Position of Suggestion Panel among left, top, right, bottom
	 */
	public CustomSuggestionAutoCompleterTextBox(List<ICustomSuggestion> suggestions, PanelPosition position) {
		super(createSuggestions(suggestions), position);
		this.suggestions = suggestions;
	}
	/**
	 * @param suggestions list of objects of {@link ICustomSuggestion} 
	 * @param isComparisonCaseSensitive if the String comparison needs to be case sensitive
	 * @param position Position of Suggestion Panel among left, top, right, bottom
	 */
	public CustomSuggestionAutoCompleterTextBox(List<ICustomSuggestion> suggestions, boolean isComparisonCaseSensitive
			, PanelPosition position) {
		super(createSuggestions(suggestions), isComparisonCaseSensitive, position);
		this.suggestions = suggestions;
	}
	/**
	 * @param suggestions list of objects of {@link ICustomSuggestion}
	 * @param isComparisonCaseSensitive if the String comparison needs to be case sensitive. Default: false
	 * @param isComparisonStartsFromBeginning if the comparison should begin from the beginning of the string. 
	 * Default: false
	 * @param position Position of Suggestion Panel among left, top, right, bottom
	 */
	public CustomSuggestionAutoCompleterTextBox(List<ICustomSuggestion> suggestions, boolean isComparisonCaseSensitive,
			boolean isComparisonStartsFromBeginning, PanelPosition position) {
		super(createSuggestions(suggestions), isComparisonCaseSensitive, isComparisonStartsFromBeginning,
				position);
		this.suggestions = suggestions;
	}
	/**
	 * @param suggestions list of objects of {@link ICustomSuggestion} 
	 */
	public CustomSuggestionAutoCompleterTextBox(List<ICustomSuggestion> suggestions) {
		super(createSuggestions(suggestions));
		this.suggestions = suggestions;
	}
	protected CustomSuggestionAutoCompleterTextBox(TextBox textBox, final List<String> suggestions, PanelPosition position){
		this(textBox, textBox, suggestions, position);
	}
	protected CustomSuggestionAutoCompleterTextBox(final TextBox textBox, Widget textboxContainer, final List<String> suggestions
			, PanelPosition position){
		super(textBox, textboxContainer, suggestions, position);
	}
	private static List<String> createSuggestions(List<ICustomSuggestion> suggestions) {
		List<String> suggestionStrings = new ArrayList<String>();
		for (int i = 0; i < suggestions.size(); i++)
			suggestionStrings.add(suggestions.get(i).getValueString());
		return suggestionStrings;
	}
	@Override
	protected String getSuggestionFilterKey(int loopIndex) {
		return suggestions.get(loopIndex).getValueString();
	}
	@Override
	protected HTML getSuggestionHtml(int loopIndex, int position, String currentTextValue) {
		return new HTML(suggestions.get(loopIndex).getDisplayString(position, currentTextValue));
	}
	@SuppressWarnings("rawtypes")
	@Override
	public List getSuggestions() {
		return suggestions;
	}
	@SuppressWarnings("unchecked")
	@Override
	public void setSuggestions(@SuppressWarnings("rawtypes") List suggestions) {
		this.suggestions = suggestions;
		prepareSuggestions();
	}
}
